<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="show">
      <span class="value" v-if="fullLocation">{{ fullLocation }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="isShow">
      <div v-if="!currList.length" class="loading"></div>
      <span
        class="ellipsis"
        @click="toggle(item)"
        v-for="item in currList"
        :key="item.code"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<script setup name="XtxCity">
import { ref, defineProps, computed, defineEmits } from "vue";
import { onClickOutside } from "@vueuse/core";
import axios from "axios";
defineProps({
  fullLocation: {
    type: String
  }
});
const emit = defineEmits(["change"]);
// 完整数据列表
const DataList = ref([]);
// 渲染数据列表
const currList = computed(() => {
  let list = DataList.value;
  if (address.value.provinceCode) list = list.find((item) => item.code === address.value.provinceCode).areaList
  if (address.value.cityCode) list = list.find((item) => item.code === address.value.cityCode).areaList
  return list;
});
const isShow = ref(false);
const hide = () => {
  isShow.value = false;
};
const show = async () => {
  isShow.value = true;
  const data = await getCityList();
  DataList.value = data;
  for (const key in address.value) {
    address.value[key] = "";
  }
};
const address = ref({
  provinceCode: "",
  provinceName: "",
  cityCode: "",
  cityName: "",
  countyCode: "",
  countyName: "",
  fullLocation: ""
});
const toggle = (item) => {
  // 省份：level === 0
  if (item.level === 0) {
    address.value.provinceCode = item.code;
    address.value.provinceName = item.name;
  }
  // 市：level === 1
  else if (item.level === 1) {
    address.value.cityCode = item.code;
    address.value.cityName = item.name;
  }
  // 区/县：level === 2
  else if (item.level === 2) {
    address.value.countyCode = item.code;
    address.value.countyName = item.name;
    hide();
    address.value.fullLocation = `${address.value.provinceName} ${address.value.cityName} ${address.value.countyName}`;
    emit("change", address);
  }
};

// 点击蒙版
const target = ref(null);
onClickOutside(target, () => {
  hide();
});

// 请求城市地区数组API
const getCityList = async () => {
  if (window.cityList) {
    return window.cityList;
  } else {
    const { data } = await axios.get(
      "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
    );
    window.cityList = data;
    return data;
  }
};
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
